package xj.graph2d.tree;

import java.awt.BorderLayout;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import xj.graph2d.DocumentChangeListener;
import xj.graph2d.DrawShape;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.app.GraphApp;

public class TabbedDocumentTreePanel 
  extends JPanel 
  implements DocumentChangeListener {

  public TabbedDocumentTreePanel(GraphApp main, Map icons) {
    this.main = main;
    trees = new DocumentTreePane[2];
    labels = new String[2];

    trees[0] = new GraphDocumentTreePane(main, icons);
    labels[0] = "graph";
    trees[1] = new DocumentTreePane(main, icons);
    labels[1] = "shapes";

    JTabbedPane tabbedPane = new JTabbedPane();
    int n = 2;
    for (int i = 0; i < n; i++) {
      tabbedPane.add(labels[i], trees[i]);
    }
    setLayout(new BorderLayout());
    add(tabbedPane, BorderLayout.CENTER);

    SimpleDrawDocument doc = main.getDoc();
    if (doc != null) {
      doc.setDocumentChangeListener(this);
    }

  }

  protected DocumentTreePane[] trees;

  protected String[] labels;

  protected GraphApp main;

  public void shapeChanged(DrawShape shape) {
    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].shapeChanged(shape);
      }
    }
  }

  public void shapeSelected(DrawShape shape) {
    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].shapeSelected(shape);
      }
    }
  }

  public void shapeAdded(DrawShape shape) {
    // System.out.println("TabbedDocumentTreePanel.shapeAdded()");

    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].shapeAdded(shape);
      }
    }
  }

  public void shapeRemoved(DrawShape shape) {
    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].shapeRemoved(shape);
      }
    }
  }

  public void selectionSetUpdated() {
    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].selectionSetUpdated();
      }
    }
  }

  public void clear() {
    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].clear();
      }
    }
  }

  public void refresh() {
    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].refresh();
      }
    }
  }

  public void reset() {
    int n = 2;
    for (int i = 0; i < n; i++) {
      if (trees[i] != null) {
	trees[i].reset();
      }
    }
    if (main != null) {
      SimpleDrawDocument doc = main.getDoc();
      if (doc != null) {
	doc.setDocumentChangeListener(this);
      }
    }
  }

}
